package cn.tedu.coolshark.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UploadController {
    @Value("${dirPath}")
    private String dirPath;
    @RequestMapping("/upload")
    public String upload(MultipartFile pic) throws IOException {
        String filename = pic.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID()+suffix;
        File dirFile = new File(dirPath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        String filePath = dirPath+"/"+filename;
        pic.transferTo(new File(filePath));
        return "/"+filename;
    }
    @RequestMapping("/remove")
    public void remove(String url){
        new File(dirPath+url).delete();
    }

}
